﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotvaBot.RequestChainItems.Base;
using Common.Constants;
using HAP = HtmlAgilityPack;
using HtmlParsing;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class DrinkSinkaChainItem : BotvaLogedInChainItemBase
    {
        [ValidatePageTitle(Title = "Персонаж. Ботва Онлайн | Битва за реальную капусту!")]
        protected override bool IsContextValidInternal()
        {
            return base.IsContextValidInternal();
        }

        protected override void PrepareRequestInternal()
        {
            HAP.HtmlDocument html = new HAP.HtmlDocument();
            html.OptionFixNestedTags = true;
            html.LoadHtml(Chain.LastContent);

            HAP.HtmlNode node = html.DocumentNode.SelectSingleNode("//*[@id=\"potions\"]");
            HAP.HtmlNode ahrefNode = node.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes[1];
            
            //?drink=1&k=28691
            string href = ahrefNode.Attributes["href"].Value;
            UrlInfo ui = UrlParser.Parse(href);
            string k = ui.QueryArgs["k"].First();

            AddGet("drink", "2");
            AddGet("k", k);

            Url = "http://g3.botva.ru/index.php";
            Method = HttpMethod.GET;
        }


        public override bool IsResponseValidInternal()
        {
            //string content = Requester.PageContent;
            //content = content.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //string searchedStr = "<input type='submit' class='cmd_all cmd_medium_sl cmd_amedium_sl ' value='ВХОД' >";
            //searchedStr = searchedStr.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //return content.Contains(searchedStr);

            return true;
        }
    }
}
